package services;

import java.util.Collection;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import domain.Bulletin;

import repositories.BulletinRepository;


@Service
@Transactional
public class BulletinService {
	
	// Managed repository -----------------------
	
	@Autowired
	private BulletinRepository bulletinRepository;
	
	// Supporting services -----------------------
	
	
	// Simpled CRUD methods ----------------------
	
	public Bulletin findOne(int bulletinId){
		return bulletinRepository.findOne(bulletinId);
	}
	
	public void save(Bulletin bulletin){
		Date date = new Date(System.currentTimeMillis()-1000);
		bulletin.setMoment(date);
		bulletinRepository.save(bulletin);
	}
	
	public void delete(Bulletin bulletin){
		bulletinRepository.delete(bulletin);
	}
	
	public Bulletin create(){
		Bulletin res = new Bulletin();
		res.setMoment(new Date());
		return res;
	}
	
	//---------------
	
	public Collection<Bulletin> findAllOrderByMomentDescending(){
		return bulletinRepository.findAllOrderByMomentDescending();
	}

}
